import axios from 'axios';
import * as constants from './constants';

const changeDetail = (data) => ({
  type: constants.GET_ORDERDATA,
  orderData: data
});

// 初始加载获取OrderTable所需数据 orderData
export const getOrderData = () => {
  return (dispatch) => {
    axios.get('http://localhost:9080/order/all?start=0&length=10', {
      headers: {
        'Authorization': "Bearer " + localStorage.getItem("access_token")
      }
    }).then((res) => {
      console.log(res.data.data);
      dispatch(changeDetail(res.data.data));
    }).catch(() => {
      console.log('error');
    })
  }
}

// dialog part
export const onOpen = (record) => ({
  type: constants.OPEN_DIALOG,
  dialogVisible: true,
  dialogData: record
});

export const onClose = () => ({
  type: constants.CLOSE_DIALOG,
  dialogVisible: false
});

// search part
export const searchFormChange = (value) => ({
  type: constants.SEARCHFORM_CHANGE,
  searchId: value.id
});

export const searchOrderById = (id) => {
  return (dispatch) => {
    axios.get('http://localhost:9080/order/search?start=0&length=10&id='+id, {
      headers: {
        'Authorization': "Bearer " + localStorage.getItem("access_token")
      }
    }).then((res) => {
      if(res.data == '') {
        alert('未搜索到结果');
      }else {
        console.log(res.data.data);
        dispatch(changeDetail(res.data.data));
      }
    }).catch(()=>{
      console.log('error');
    })
  }
};